package teleware.soe.template;

import com.esri.arcgis.carto.IMapLayerInfo;
import com.esri.arcgis.carto.IMapLayerInfos;
import com.esri.arcgis.carto.IMapServerDataAccess;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.server.IServerObjectHelper;
import teleware.soe.core.annotation.Controller;
import teleware.soe.core.annotation.OperationMapping;
import teleware.soe.core.annotation.RequestParam;
import teleware.soe.core.annotation.ResourceMapping;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :hongsw
 * @date :2020-12-10 19:39
 */
@Controller
public class HandleController {
    @ResourceMapping(value = "layers", description = "layers in associated map service", isCollection = false, isDefaultCollection = false)
    public Map<String, Object> getSubresourceLayers(IMapLayerInfos layerInfos, IServerObjectHelper soHelper) throws IOException {
        Map<String, Object> rootMap = new HashMap<String, Object>();
        int count = layerInfos.getCount();
        rootMap.put("layerCount", count);
        for (int i = 0; i < count; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            IMapLayerInfo layerInfo = layerInfos.getElement(i);
            map.put("name", layerInfo.getName());
            String layerType = layerInfo.getType();
            map.put("type", layerType);
            int id = layerInfo.getID();
            map.put("id", id);
            map.put("description", layerInfo.getDescription());
            if (layerInfo.isFeatureLayer()) {
                IMapServerDataAccess mapServerDataAccess = (IMapServerDataAccess) soHelper.getServerObject();
                FeatureClass fc = new FeatureClass(
                        mapServerDataAccess.getDataSource("", id));
                map.put("featureCount", fc.featureCount(null));
            }
            rootMap.put(String.valueOf(i), map);
        }
        return rootMap;
    }


    @OperationMapping(value = "getLayerCountByType", supportedOutputFormats = "json", postOnly = false)
    public Map<String, Object> getLayerCountByType(@RequestParam("type") String type, IMapLayerInfos layerInfos) throws IOException {
        int count = 0;
        if (type != null && !type.isEmpty()) {
            String aoType = "";
            if ("all".equalsIgnoreCase(type)) {
                count = layerInfos.getCount();
            } else if ("feature".equalsIgnoreCase(type)) {
                aoType = "Feature Layer";
            } else if ("raster".equalsIgnoreCase(type)) {
                aoType = "Raster Layer";
            } else if ("dataset".equalsIgnoreCase(type)) {
                aoType = "Network Dataset Layer";
            }

            for (int i = 0; i < layerInfos.getCount(); i++) {
                if (layerInfos.getElement(i).getType().equalsIgnoreCase(aoType)) {
                    count++;
                }
            }
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("count", count);
        return result;
    }
}